﻿using System.Windows;

namespace ChangeThemes
{
    public class ThemeManage
    {
        private Dictionary<string, ResourceDictionary> _themeDis = new();

        /// <summary>
        /// pack://application:,,,/FilterDataGrid;component/Themes/FilterDataGrid.xaml
        /// </summary>
        /// <param name="themeName"></param>
        /// <param name="assembly"></param>
        /// <param name="resourcePath"></param>
        public void RegisterTheme(string themeName, string assembly, string resourcePath)
        {
            var url = $"/{assembly};component/{resourcePath}";
            var resource = new ResourceDictionary();
            resource.Source = new Uri(url, UriKind.RelativeOrAbsolute);
            if (!_themeDis.ContainsKey(themeName))
            {
                _themeDis.Add(themeName, resource);
            }
        }

        public void ApplyTheme(string themeName)
        {
            if (!_themeDis.TryGetValue(themeName, out ResourceDictionary? value))
            {
                return;
            }
            var resource = value;
            if (Application.Current.Resources.MergedDictionaries.Contains(resource))
            {
                Application.Current.Resources.MergedDictionaries.Remove(resource);
            }
            Application.Current.Resources.MergedDictionaries.Add(resource);
        }
    }
}
